package cate.game.play.skill.passive.gem;

import cate.common.util.XT;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.skill.passive.PassiveHandler;
import easy.java.struct.ref.DoubleRef;

import java.util.HashMap;
import java.util.Map;

/**
 * 受击时，有 46% 承受本次伤害的 42% ，每个回合结束时损失 12% 伤害，持续 2 回合
 */
public class 坚挺PH extends PassiveHandler {

	private int 概率;
	private double 承受比例;
	private double 损失比例;
	private int 回合;

	//概率=4600&承受比例=4200&损失比例=1200&回合=2
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		概率 = args.getInt("概率", 0);
		承受比例 = args.getWDouble("承受比例", 0d);
		损失比例 = args.getWDouble("损失比例", 0d);
		回合 = args.getInt("回合", 0);
	}

	private Map<Integer,Double> damages = new HashMap<>();

	@Override
	public void adjustSkillDamage(ActionCtx action, Fighter actor, int reason, DoubleRef doubleRef) {
		if (!XT.chance(概率)) {
			return;
		}
		double damage = doubleRef.v * 损失比例 + damages.getOrDefault(action.getRound().index, 0d);
		damages.put(action.getRound().index, damage);
		doubleRef.v *= 承受比例;
	}

	@Override
	public void onRoundEnd(ActionCtx action) {
		double total = 0d;
		for (int i = 1; i <= 回合; i++) {
			total += damages.getOrDefault(action.getRound().index - (i - 1), 0d);
		}
		if (total > 0) {
			skill.owner.attr.passiveHurt(action, skill.owner, total);
		}
	}
}
